package com.gugee.tiktok.web.repository.impl;

import com.gugee.tiktok.web.dto.ChallengeResponse;
import com.gugee.tiktok.web.model.es.ESVideo;
import com.gugee.tiktok.web.model.es.ESWeekGrowth;
import com.gugee.tiktok.web.repository.ChallengeCollectionExtRepository;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.boot.toolkit.jdbc.JdbcHolder;
import com.xiguaji.boot.toolkit.jdbc.Page;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChallengeCollectionExtRepositoryImpl implements ChallengeCollectionExtRepository {
    @Override
    public Page<ChallengeResponse> getPage(int page, int pageSize, String keyword, int sort, Long userId) {
        String selectSql = "select c.id as collectionId,ch.* ";
        StringBuilder sql = new StringBuilder(" from challenge_collection c left join challenge ch on c.cid = ch.cid ");
        sql.append("where c.user_id = ? ");
        List<Object> params = new ArrayList<>();
        params.add(userId);

        if(!StringUtils.isEmpty(keyword)){
            sql.append(" and (ch.cha_name like ? or ch.desc like ?) ");
            params.add("%"+keyword+"%");
            params.add("%"+keyword+"%");
        }


        String sortStr = " order by ch.user_growth desc";
        if(sort != 1){
            sortStr = " order by ch.view_growth desc";
        }


        sql.append(sortStr);

        Page<ChallengeResponse> pageResponse = new Page(null,0,0,0,0);

        int totalRow = JdbcHolder.getJdbcTemplate().queryForObject("select count(*) "+sql.toString(), params.toArray(), Integer.class);

        if(totalRow == 0){
            return pageResponse;
        }
        List<Map<String,Object>> lists = JdbcHolder.getJdbcTemplate().queryForList(selectSql+sql.toString(), params.toArray());
        List<ChallengeResponse> challengeResponses = new ArrayList<>();

        lists.forEach( m -> {
            ChallengeResponse challengeResponse = new ChallengeResponse();
            challengeResponse.setCollectId((Long) m.get("collectionId"));
            challengeResponse.setFavorite(true);
            challengeResponse.setChaName(m.get("cha_name").toString());
            challengeResponse.setCid((Long)m.get("cid"));
            challengeResponse.setCommerce((Boolean) m.get("is_commerce"));
            challengeResponse.setCoverPhoto(m.get("cover_photo").toString());
            challengeResponse.setDesc((String) m.get("desc"));
            challengeResponse.setQrCode((String) m.get("qr_code"));
            challengeResponse.setRegion((String) m.get("region"));
            challengeResponse.setShareUrl((String) m.get("share_url"));
            challengeResponse.setUserCount((Long) m.get("user_count"));
            challengeResponse.setUserGrowth((Long) m.get("user_growth"));
            challengeResponse.setViewCount((Long) m.get("view_count"));
            challengeResponse.setViewGrowth((Long) m.get("view_growth"));

            if(!StringUtils.isEmpty(m.get("week_growth"))){
                List<ESWeekGrowth> esDayVideoGrowths = JsonUtil.jsonParse(List.class,m.get("week_growth").toString());
                challengeResponse.setWeekGrowth(esDayVideoGrowths);
            }
            if(!StringUtils.isEmpty(m.get("top_videos"))){
                List<ESVideo> esDayVideoGrowths = JsonUtil.jsonParse(List.class,m.get("top_videos").toString());
                challengeResponse.setTopVideos(esDayVideoGrowths);
            }
            challengeResponses.add(challengeResponse);
        });
        pageResponse = new Page(challengeResponses,totalRow,0,0,0);

        return pageResponse;
    }
}
